//
//  ChooseVoucher.m
//  DaoZiJinFu
//
//  Created by woker on 16/6/24.
//  Copyright © 2016年 iwoker. All rights reserved.
//

#import "ChooseVoucher.h"
#import "ChooseVoucherCell.h"
#import "ChooseVoucherHeaderView.h"
#import "VoucherExplainTableVC.h"
#import "VoucherModel.h"

#define cellH 50*(SCREEN_WIDTH-24)/150+16

@interface ChooseVoucher () {
    NSMutableArray *usableArr;
    NSMutableArray *unusableArr;
}

@end

@implementation ChooseVoucher

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTitleView:@"选择抵用券"];
    self.view.backgroundColor = BACKGROUND_COLOR;
    [self leftButton:nil];
    [self rightButton];
    
    usableArr = [NSMutableArray array];
    unusableArr = [NSMutableArray array];
    
    for (VoucherModel *model in _voucherArray) {
        if ([model.limit_amount doubleValue] <= [_investMoney doubleValue]) {
            [usableArr addObject:model];
        } else {
            [unusableArr addObject:model];
        }
    }
    
    [_voucherArray removeAllObjects];
    [_voucherArray addObjectsFromArray:usableArr];
    [_voucherArray addObjectsFromArray:unusableArr];
    [self.tableView reloadData];
}

- (void)rightBarItemOnClick:(id)sender {
    VoucherExplainTableVC *voucherExplain = [[VoucherExplainTableVC alloc] init];
    [self.navigationController pushViewController:voucherExplain animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSInteger count = 0;
    for (VoucherModel *model in _voucherArray) {
        if ([model.limit_amount doubleValue] <= [_investMoney doubleValue]){
            count ++;
        }
    }
    return count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerNib:[UINib nibWithNibName:@"ChooseVoucherCell" bundle:nil] forCellReuseIdentifier:CHOOSEVOUCHER_CELL];
    ChooseVoucherCell *cell = [ChooseVoucherCell cellWithTableView:tableView];
    VoucherModel *model = _voucherArray[indexPath.row];
    
    cell.voucherModel = model;
    cell.voucherLimitDay.text = [NSString stringWithFormat:@"适用于%.0lf个月及以上理财产品",[model.limit_day doubleValue]/30.0];
    cell.deadlineLab.text = [NSString stringWithFormat:@"有效期至%@",model.expire_time];
    cell.useCondition.text = model.name;
    UIColor *white = [UIColor whiteColor];
    NSString *valurStr = [NSString stringWithFormat:@"%ld",[model.amount integerValue]];
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@元",valurStr]];
    NSDictionary *amountAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:30.0f],NSForegroundColorAttributeName:(([model.limit_amount doubleValue] <= [_investMoney doubleValue]) ? white : white)};
    NSDictionary *yuanAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:15.0f],NSForegroundColorAttributeName:(([model.limit_amount doubleValue] <= [_investMoney doubleValue]) ? white : white)};
    [attrStr setAttributes:amountAttr range:NSMakeRange(0, valurStr.length)];
    [attrStr setAttributes:yuanAttr range:NSMakeRange(valurStr.length, 1)];
    cell.voucherValue.attributedText = [[NSAttributedString alloc] initWithAttributedString:attrStr];
    
    NSString *imgName = @"";
    cell.voucherFrom.text = cell.voucherModel.source_title;
    if ([model.limit_amount doubleValue] <= [_investMoney doubleValue]) {
        cell.voucherFrom.textColor = white;
        imgName = @"可用券";
    } else {
        cell.voucherFrom.textColor = white;
        imgName = @"不可用券";
    }
    cell.backgroundImg.image = [UIImage imageNamed:imgName];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return cellH;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}
//- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section; {
//    return [ChooseVoucherHeaderView loadView];
//}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < usableArr.count) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"voucherChanged" object:self userInfo:@{@"voucherModel":_voucherArray[indexPath.row]}];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
